<?php
/*
+--------------------------------------------------------------------------
|   phpBIZ v3.0  full version
|   ========================================
|   by taft@wjl.cn yejun@wjl.cn
|   http://www.phpbiz.cn http://www.wjl.cn
|   all rights reserved
+---------------------------------------------------------------------------
|
|   > 最后修改日期：2005-4-30 2006-2-8
|
+--------------------------------------------------------------------------
*/
!class_exists('Debug') && exit('Forbidden');
$idx = new Search;
class Search
{
	function Search()
	{
		global $BIZ,$INCOME;
		
		if($INCOME[code]=='dosearch')
		{
			$this -> do_search();
			exit;
		}
		//-------------------------------------------
		//read cat info
		//-------------------------------------------
		include 'cache/cat_cache.php';   
		
		$c_r=$_CAT[0];
		$cat_id[] ="all";
		$cat_name[] ="所有分类";
		foreach( $c_r as $k=>$v )
		{
			if($c_r[$k][1])
			{
				$cat_id[] = $c_r[$k][1];
				$cat_name[] =$c_r[$k][0];
			}else{
				$cat_id[] = $c_r[$k][3];
				$cat_name[] = "&nbsp;&nbsp;&nbsp;|-".$c_r[$k][2];
			}
		}

		$BIZ -> assign("cat_id",$cat_id);
		$BIZ -> assign("cat_name",$cat_name);
		//--------------------------------------------------------

		$BIZ -> output("Search.tpl");
	}
	function do_search()
	{
		global $INCOME,$DB,$BIZ;
		define("PAGE_NUM",15);
		
		if( $INCOME['type']=='searchg' )
		{
            $INCOME[goods_name] = $INCOME['searchword'];

		}else if( $INCOME['type']=='searchs' )
		{
			$INCOME[shop_name] = $INCOME['searchword'];
			$INCOME[shopcondition] = 'shop_name';
		}
		
		if( empty($INCOME[shop_name]) && empty($INCOME[goods_name]))
		             $BIZ->error('错误：没有指定搜索条件');
		$INCOME[shop_name] && $INCOME[goods_name] && $BIZ->error("错误：不能同时搜索商品和商铺。");
        
		//------------------------------
		//build search condition
        //------------------------------
		

		if($INCOME[shop_name])
		{
			$orderway = (!$INCOME[orderway])?"shop_create_time":"shop_visit_count";
			$sqltable = "biz_shop";
		}
		else
		{
			$orderway = (!$INCOME[orderway])?"goods_store_date":"goods_visit_count";
			$sqltable = "biz_goods";
		}
		
		$orderrank = $INCOME[asc]? $INCOME[asc]:'DESC';
		$orderby="ORDER BY $orderway $orderrank";
		
		$method=$INCOME[method]=='AND'? 'AND' :'OR';

		$sqlwhere =" 1 ";
		is_numeric($INCOME[cid]) && $INCOME[goods_name] && $sqlwhere="goods_cat_id  IN ($INCOME[cid]) ";
		if(is_numeric($INCOME[sch_time]) && strlen($INCOME[sch_time])<10)
		{
			$sch_time=time()-$INCOME[sch_time];
			if($INCOME[shop_name]) $sqlwhere.=" AND shop_create_time>'$sch_time'";
			else $sqlwhere.=" AND goods_store_date>'$sch_time'";
		}

		if($INCOME[goods_name])
		{
			$INCOME[goods_name]=str_replace("%",'\%',$INCOME[goods_name]);
			$INCOME[goods_name]=str_replace("_",'\_',$INCOME[goods_name]);
			$gname_db = explode("|",$INCOME[goods_name]);

			foreach($gname_db as $value)
			{
				if($value)
				{
					$value=addslashes(trim($value));
					$keywhere.=$method;
					$keywhere.=" goods_name LIKE '%$value%'";
				}
			}
		}
		if($INCOME[shop_name])
		{
			$INCOME[shop_name]=str_replace("%",'\%',$INCOME[shop_name]);
			$INCOME[shop_name]=str_replace("_",'\_',$INCOME[shop_name]);
			$shop_db = explode("|",$INCOME[shop_name]);
			foreach($shop_db as $value)
			{
				if($value)
				{
					$value=addslashes(trim($value));
					$keywhere.=$method;
					$keywhere.=" $INCOME[shopcondition] LIKE '%$value%'";
				}
			}
		}
		if($keywhere)
		{
			$keywhere=substr_replace($keywhere,"",0,3);
			$keywhere && $sqlwhere.="AND ($keywhere)";
		} 
        //--------------------------------------------------

		$start = $INCOME['st'] ? intval( $INCOME['st'] ) : 0;
		$DB -> db_query("SELECT count(*) as count FROM $sqltable WHERE $sqlwhere");
		$row_count = $DB -> db_fetch_row();

		if ($row_count['count'] < 1)
				$BIZ->error("没有从数据库中返回任何匹配。","");

		$shop_name=rawurlencode($INCOME['shop_name']);
		$goods_name=rawurlencode($INCOME['goods_name']);
		$pages = $BIZ->build_pagelinks( array('TOTAL_POSS'  => $row_count['count'],
											   'PER_PAGE'    => PAGE_NUM,
											   'CUR_ST_VAL'  => $start,
											   'L_SINGLE'    => "一页",
											   'L_MULTI'     => "页：",
											   'BASE_URL'    => $this->base_url."?act=Search&code={$INCOME['code']}&goods_name={$goods_name}&shop_name={$shop_name}&method={$INCOME[method]}&shopcondition={$INCOME[shopcondition]}&cid={$INCOME[cid]}&sch_time={$INCOME[sch_time]}&orderway={$INCOME[orderway]}&asc={$INCOME[asc]}"
											   )
											);

		$query = "SELECT * FROM $sqltable WHERE $sqlwhere $orderby LIMIT $start,".PAGE_NUM;
		$DB -> db_query( $query);
	    while( $r=$DB -> db_fetch_row() )
		{
			
			if($sqltable=="biz_shop")
			{
				$tm = date("Y-m-d H:i",$r['shop_create_time']);
				$r[shop_name]= str_replace($INCOME['shop_name'],"<font class=red>".$INCOME['shop_name']."</font>",$r[shop_name]);
				$r[shop_location] = str_replace($INCOME['shop_name'],"<font class=red>".$INCOME['shop_name']."</font>",$r[shop_location]);
				$result[]=array($r[shop_id],$r[shop_name],$r[shop_catalog],$r[shop_location],$tm);
			    $flag="shop";
			}
			else
			{
				$tm = date("Y-m-d H:i",$r['goods_store_date']);
				$r[goods_name]= str_replace(trim($INCOME['goods_name']),"<font class=red>".trim($INCOME['goods_name'])."</font>",$r[goods_name]);
				$result[]=array($r[goods_id],$r[goods_name],$r[goods_price],$r[goods_visit_count],$tm);
				$flag="goods";
			}
		}
        $BIZ ->assign("total",$row_count['count']);
		$BIZ ->assign("result",$result);
		$BIZ ->assign("flag",$flag);
		$BIZ ->assign("pages",$pages);
		$BIZ ->output("Result.tpl");
	}
}
?>